package com.chencong.service;

import com.alibaba.fastjson.JSONObject;
import com.chencong.entity.Bill;
import com.chencong.entity.Provider;
import com.chencong.request.Request;
import com.chencong.util.Constant;

import java.io.DataOutputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chencong
 * @date 2020/12/15 下午1:32
 */
public class BillService {
    private DataOutputStream dos;
    private Request request;
    private int maxID = -1;
    private PageService ps;//用来弹出页面信息
    private FileService fs;//用来向文件读写数据

    public BillService(DataOutputStream dos, Request request) {
        this.dos = dos;
        this.request = request;
        this.ps = new PageService(dos);
        this.fs = new FileService();
    }

    /**
     * 获取账单列表
     */
    public void getBillList() throws Exception {
//        数据说明：根据product进行商品描述模糊查询，根据isPay对商品是否付款进行查询
//        当product对应字符为空字符的时候，表示任意商品描述均满足。
//        当isPay为-1是，表示认识付款均满足条件。（1. 已付款，0.未付款）
        String data = request.getData();
        Bill bill = JSONObject.parseObject(data, Bill.class);
        List<Bill> bills = loadBill();
        //是否付款来查询
        if (bill.getIsPay() != -1) {
            List<Bill> collect = bills.stream().filter(bill1 -> bill1.getIsPay() == bill.getIsPay()).collect(Collectors.toList());
            bills = collect;
        }
        //根据商品描述来查询
        if (bill.getProduct().length() != 0) {
            List<Bill> collect = bills.stream().filter(bill1 -> bill1.getProduct().contains(bill.getProduct())).collect(Collectors.toList());
            bills = collect;
        }
//        System.out.println("获取账单列表"+bills);
        ps.sendData(bills);
    }

    /**
     * 加载账单数据库
     *
     * @return
     */
    public List<Bill> loadBill() {
        String json = fs.read(Constant.SRC_BILLS);
        List<Bill> bills = JSONObject.parseArray(json, Bill.class);
        if (maxID == -1) {
            maxID = bills.stream().max((o1, o2) -> o1.getId() - o2.getId()).get().getId();
        }
        return bills;
    }

    public boolean addBill(String data) throws Exception {
        Bill bill = JSONObject.parseObject(data, Bill.class);
        System.out.println(bill);
        //校验账单是否合法
        String s = checkBill(bill);
        if (!s.equals("true")) {
            ps.sendData(s, false);
            return false;
        }
        List<Bill> bills = loadBill();
        bill.setId(++maxID);
        bill.setIsPayStr();
        bill.setUpdateTime();
        bill.setProviderName();
        System.out.println(bill);
        bills.add(bill);
        fs.write(Constant.SRC_BILLS, bills);
        ps.sendData("操作成功");
        return true;
    }

    /**
     * 校验账单是否合法
     *
     * @param bill
     * @return
     */
    public String checkBill(Bill bill) {
        if (bill.getMoney() <= 0) {
            System.out.println("请输入正确的交易金额");
            return "请输入正确的交易金额";
        } else if (bill.getProduct().length() == 0) {
            System.out.println("商品描述不能为空");
            return "商品描述不能为空";
        }
        return "true";
    }
    /**
     * 账单信息查询
     *
     * @param data
     */
    public void getBill(String data) throws Exception {
        Bill bill = JSONObject.parseObject(data, Bill.class);
        List<Bill> bills = loadBill();
        List<Bill> collect = bills.stream().filter(bill1 -> bill1.getId() == bill.getId()).collect(Collectors.toList());
        ps.sendData(collect.get(0));
    }

    /**
     * 账单信息修改
     *
     * @param data
     */
    public void updateBill(String data) throws Exception {
        Bill bill = JSONObject.parseObject(data, Bill.class);
        //校验账单是否合法
        String s = checkBill(bill);
        if (!s.equals("true")) {
            ps.sendData(s, false);
            return;
        }
        List<Bill> bills = loadBill();
        for (int i = 0; i < bills.size(); i++) {
            if (bills.get(i).getId() == bill.getId()) {
                bills.set(i, bill);
                break;
            }
        }
        System.out.println(bills);
        fs.write(Constant.SRC_BILLS, bills);
        ps.sendData("操作成功");
    }

    /**
     * 删除账单
     *
     * @param data
     */
    public void deleteBill(String data) throws Exception {
        Bill bill = JSONObject.parseObject(data, Bill.class);
        List<Bill> bills = loadBill();
        Bill bill2 = bills.stream().filter(bill1 -> bill1.getId() == bill.getId()).findFirst().get();
        bills.remove(bill2);
        fs.write(Constant.SRC_BILLS, bills);
        ps.sendData("操作成功");
    }

}
